var prefix = jsctx + "/hlhtTenantOpenApply";

var hlhtTenantOpenApply = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        },10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 80;
    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageHlhtTenantOpenApply",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    cache: false,
                    height: h,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    sidePagination: "server",
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            hlhtType:$("#hlhtType").val().trim(),
                            hlhtOpenType:$("#hlhtOpenType").val().trim(),
                            // searchName:$("#searchName").val().trim(),
                            hlhtTenantOrHlhtOperator:$("#hlhtTenantOrHlhtOperator").val()
                            //elecStationId: $("#elecStationId").val().trim(),
                            //  operateStatus: $("#operateStatus").val().trim(),
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '',width: '50',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'tenantCode', title: '本方运营商编码', align: 'left', width: '100'},
                        {field: 'tenantName', title: '本方运营商', align: 'left', width: '100'},
                        {
                            field: 'code', title: '对方运营商编码', align: 'left', width: '100',
                            formatter: function (value, row, index) {
                                if (row.hlhtOpenType == '0') {
                                    return row.hlhtTenantCode;
                                } else {
                                    return row.hlhtOperatorId;
                                }
                            }

                        },
                        {
                            field: 'name', title: '对方运营商', align: 'left', width: '100',
                            formatter: function (value, row, index) {
                                if (row.hlhtOpenType == '0') {
                                    return row.hlhtTenantName;
                                } else {
                                    return row.hlhtOperatorName;
                                }
                            }

                        },
                        {
                            field: 'hlhtOpenType', title: '互联类型', align: 'center',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span class="label label-info">SAAS运营商</span>'
                                } else if (value == '1') {
                                    return '<span class="label label-danger">商业运营平台</span>'
                                } else if (value == '2') {
                                    return '<span class="label label-danger">政府平台</span>'
                                }
                            }
                        },
                        {
                            field: 'hlhtType', title: '互联方向', align: 'center',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span class="label label-info">反向</span>'
                                } else if (value == '1') {
                                    return '<span class="label label-danger">正向</span>'
                                }
                            }
                        },
                        {field: 'beginDate', title: '互联开始日期', align: 'left'},
                        {field: 'endDate', title: '互联结束日期', align: 'left'},
                        {
                            field: 'status', title: '审核状态', align: 'left',
                            formatter: function (value, row, index) {
                                if (row.status == '0') {
                                    return '未审核';
                                } else if (row.status == '1') {
                                    return '审核通过';
                                } else if (row.status == '2') {
                                    return '审核不通过';
                                }
                            }
                        },
                        {
                            title: '操作',
                            field: 'roleId',
                            align: 'center',
                            formatter: function (value, row, index) {
                                var g ='';
                                var a = '<a class="btn btn-primary btn-sm" ' + has_edit + '  href="#" title="审核通过" onclick="hlhtTenantOpenApply.audit(\''
                                    + row.openApplyId + '\',' + '\'' + 1 +
                                    '\')">审核通过</a> ';

                                var b = '<a class="btn btn-danger btn-sm" ' + has_edit + '  href="#" title="审核通过" onclick="hlhtTenantOpenApply.audit(\''
                                    + row.openApplyId + '\',' + '\'' + 2 +
                                    '\')">审核通过</a> ';

                                var c = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="编辑(反向)" onclick="hlhtTenantOpenApply.editHlhtTenant(\''
                                    + row.openApplyId + '\')">编辑(反向)</a> ';
                                var d = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="编辑(正向)" onclick="hlhtTenantOpenApply.editHlhtOperator(\''
                                    + row.openApplyId + '\')">编辑(正向)</a> ';
                                var e = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="已审核" >已审核</a> ';
                                if (Hlht_apply_look){
                                    g = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="查看详情" onclick="hlhtTenantOpenApply.auditHlhtTenantOpenApply(\''
                                        + row.openApplyId + '\')">查看详情</a> ';
                                }
                                return g;
                                /*if (row.status == "0") {
                                    return  g;
                                }else if(row.status == "1"){
                                    return c + d;
                                } else {
                                    return e;
                                }*/

                            }
                        }
                    ]
                });
    }
    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function audit(openApplyId, status) {
        layer.confirm('确定要更改状态？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/audit",
                type: "post",
                data: {
                    'openApplyId': openApplyId,
                    'status': status,
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("审核成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    function goBack() {
        window.history.back();
    }

    function addHlhtTenantOpenApply() {
        location.href = prefix + "/addHlhtTenantOpenApply";
    }

    function editHlhtTenant(openApplyId) {
        location.href = prefix + "/editHlhtTenant/" + openApplyId;
    }

    function editHlhtOperator(openApplyId) {
        location.href = prefix + "/editHlhtOperator/" + openApplyId;

    }

    function auditHlhtTenantOpenApply(openApplyId) {
        location.href = prefix + "/auditHlhtTenantOpenApply/" + openApplyId;
    }


    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        audit: audit,
        addHlhtTenantOpenApply: addHlhtTenantOpenApply,
        editHlhtTenant: editHlhtTenant,
        editHlhtOperator: editHlhtOperator,
        auditHlhtTenantOpenApply: auditHlhtTenantOpenApply
    };
})();

var addHlhtTenantOpenApply = (function () {
    function init() {
        validateRule();

    };

    function getCheckedRoles() {
        var adIds = "";
        $("input:checkbox[name=role]:checked").each(function (i) {
            if (0 == i) {
                adIds = $(this).val();
            } else {
                adIds += ("," + $(this).val());
            }
        });
        return adIds;
    }

    var isajaxsend = true;

    function save() {
        if (isajaxsend) {
            var formData = new FormData($("#frmHlhtTenantOpenApply")[0]);
            $("#roleIds").val(getCheckedRoles());
                var fileName = $("#fileName").val();
                var file = $("#docfile").val();
                var AllImgExt = ".doc、.docx或.pdf";
                var fileType = ".doc.docx.pdf";
                var extName = file.substring(file.lastIndexOf(".")).toLowerCase();//（把路径中的所有字母全部转换为小写）
                if (fileType.indexOf(extName) == -1) {
                    ErrMsg = "该文件类型不允许上传。请上传 " + AllImgExt + " 类型的文件";
                    parent.layer.msg(ErrMsg);
                    return false;
                }
            $.ajax({
                cache: true,
                type: "POST",
                url: prefix + "/saveOrUpdateHlhtTenantOpenApply",
                // data: $('#frmHlhtTenantOpenApply').serialize(),
                data: formData,
                processData: false,
                contentType: false,
                async: false,
                beforeSend: function () {
                    isajaxsend = false
                },
                error: function (request) {
                    parent.layer.alert("Connection error");
                },
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        window.history.back();
                    } else {
                        parent.layer.alert(data.message);

                    }

                },
                complete: function () {
                    // isajaxsend = false
                    isajaxsend=true;
                }
            });
        }
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmHlhtTenantOpenApply").validate({
            rules: {

                levelTypeName: {
                    required: true
                },
                minValue: {
                    required: true
                },
                maxValue: {
                    required: true
                },
                keepValue: {
                    required: true
                },
                expireDay: {
                    required: true
                },
                padValue: {
                    required: true
                },
                discountPercent: {
                    required: true
                }

            },
            messages: {

                levelTypeName: {
                    required: icon + "请输入等级名称"
                },
                minValue: {
                    required: icon + "请输入成长值最小值"
                },
                maxValue: {
                    required: icon + "请输入成长值最大值"
                },
                keepValue: {
                    required: icon + "请输入保级值"
                },
                expireDay: {
                    required: icon + "请输入保级有效期"
                },
                padValue: {
                    required: icon + "请输入衰减值"
                },
                discountPercent: {
                    required: icon + "请输入服务费折扣"
                }
            },
            submitHandler: function (form) {
                save();
            }
        })
    }

    return {
        init: init
    };
})();
